package com.vp.org.dao.impl;

import com.vp.org.constant.BookSql;
import com.vp.org.dao.BookDao;
import com.vp.org.pojo.BookInfo;
import com.vp.org.pojo.Collect;
import com.vp.org.util.JdbcUtil;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;

import java.util.List;
import java.util.Map;

public class BookDaoImpl extends JdbcUtil implements BookDao, BookSql {


    @Override
    public List<BookInfo> queryByHot() {
        return template.query(QUERY_BY_HOT, new BeanPropertyRowMapper<>(BookInfo.class));
    }

    @Override
    public List<BookInfo> queryByCondition(BookInfo info) {
        return template.query(QUERY_BY_CONDITION, new BeanPropertyRowMapper<>(BookInfo.class), info.getType());
    }

    @Override
    public List<BookInfo> queryByDiscount() {
        return template.query(QUERY_BY_DISCOUNT, new BeanPropertyRowMapper<>(BookInfo.class));
    }

    @Override
    public BookInfo queryById(int id) {
        return template.queryForObject(QUERY_BY_ID, new BeanPropertyRowMapper<>(BookInfo.class), id);
    }

    @Override
    public List<Map<String, Object>> queryRemarkById(int id) {
        return template.queryForList(QUERY_REMARK_BY_ID, id);
    }

    @Override
    public BookInfo queryByName(String name) {
        try {
            return template.queryForObject(QUERY_BY_NAME, new BeanPropertyRowMapper<>(BookInfo.class), name);
        } catch (DataAccessException e) {
            return null;
        }
    }

    @Override
    public int collectBook(int bookId, int userId) {
        return template.update(COLLECT_BOOK, bookId, userId);
    }

    @Override
    public List<Collect> queryAll(int id) {
        return template.query(QUERY_COLLECT, new BeanPropertyRowMapper<>(Collect.class), id);
    }

    @Override
    public int delete(int id) {
        return template.update(DELETE, id);
    }
}
